package jnpf.provider.impl;

import cn.xuyanwu.spring.file.storage.FileInfo;
import jnpf.model.FileListVO;
import jnpf.model.upload.DownFileModel;
import jnpf.model.upload.UploadFileModel;

import jnpf.provider.file.FileUploadProvider;
import jnpf.util.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.List;

/**
 * Dubbo服务提供者
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司（https://www.jnpfsoft.com）
 * @date 2021-07-27
 */
@Slf4j
@DubboService
public class FileUploadProviderImpl implements FileUploadProvider {

    @Override
    public FileInfo uploadFiles(UploadFileModel model) {
        return FileUploadUtils.uploadFile(model.getBarray(), model.getFolderName(), model.getObjectName());
//        if (model.getBarray() != null) {
//            InputStream is = new ByteArrayInputStream(model.getBarray());
//            GetStrategy.getInstance(fileCoreProperties.getDefaultPlatform()).uploadFile(model.getFilePath(), model.getFolderName(), model.getObjectName(), is);
//        } else if (model.getIs() != null){
//            GetStrategy.getInstance(fileCoreProperties.getDefaultPlatform()).uploadFile(model.getFilePath(), model.getFolderName(), model.getObjectName(), model.getIs());
//        } else {
//            GetStrategy.getInstance(fileCoreProperties.getDefaultPlatform()).uploadFiles(model.getFilePath(), model.getFolderName(), model.getObjectName());
//        }
    }

    @Override
    public void removeFile(String folderName, String fileName) {
        FileUploadUtils.removeFile(folderName, fileName);
//        GetStrategy.getInstance(fileCoreProperties.getDefaultPlatform()).removeFile(folderName, fileName);
    }

    @Override
    public boolean downFile(DownFileModel downFileModel) {
//        byte[] bytes = FileUploadUtils.downloadFileByte(downFileModel.getFolderName(), downFileModel.getObjectName());
//        FileDownloadUtil.downloadFile(bytes, downFileModel.getObjectName(), null);
        return true;
//        return GetStrategy.getInstance(fileCoreProperties.getDefaultPlatform()).downFile(downFileModel.getObjectName(), downFileModel.getFolderName());
    }

    @Override
    public boolean writeImage(DownFileModel downFileModel) {
//        byte[] bytes = FileUploadUtils.downloadFileByte(downFileModel.getFolderName(), downFileModel.getObjectName());
//        FileDownloadUtil.flushImage(bytes);
        return true;
//        return GetStrategy.getInstance(fileCoreProperties.getDefaultPlatform()).writeImage(downFileModel.getObjectName(), downFileModel.getFolderName());
    }

    @Override
    public void putFolder(UploadFileModel model) {

//        GetStrategy.getInstance(fileCoreProperties.getDefaultPlatform()).putFolder(model.getFolderName(), model.getFilePath(), model.getObjectName());
    }

    @Override
    public List<FileListVO> getFileList(String folderName) {
//        List<FileListVO> data = new LinkedList<>();
        List<FileListVO> fileList = FileUploadUtils.getFileList(folderName);
//        if (fileList != null) {
//            for (int i = 0; i < fileList.size(); i++) {
//                FileDetail fileDetail = fileList.get(0);
//                FileListVO fileModel = new FileListVO();
//                fileModel.setFileId(i + "");
//                fileModel.setFileType(fileDetail.getExt());
//                fileModel.setFileName(fileDetail.getFilename());
//                fileModel.setFileTime(DateUtil.dateFormat(fileDetail.getCreateTime()));
//                fileModel.setFileSize(fileDetail.getSize() == null ? "0" : String.valueOf(fileDetail.getSize()));
//                data.add(fileModel);
//            }
//        }
        return fileList;
    }

    @Override
    public List<FileListVO> getVisualList(String folderName) {
        return FileUploadUtils.getDefaultFileList(folderName);
    }

    @Override
    public void downToLocal(UploadFileModel model) {
        FileUploadUtils.downloadFile(model.getFolderName(), model.getObjectName());
//        GetStrategy.getInstance(fileCoreProperties.getDefaultPlatform()).downToLocal(model.getFolderName(), model.getFilePath(), model.getObjectName());
    }

    @Override
    public byte[] getInputStream(String fileName, String folderName) {
        return FileUploadUtils.downloadFileByte(folderName, fileName, false);
//        return GetStrategy.getInstance(fileCoreProperties.getDefaultPlatform()).getInputStream(fileName, folderName);
    }

    @Override
    public void downloadFile(byte[] bytes, String fileName, String name) {
        FileDownloadUtil.downloadFile(bytes, fileName, name);
    }

    @Override
    public String getDefaultPlatform() {
        return FileUploadUtils.getDefaultPlatform();
    }
}
